package de.hwrberlin.remo.algorithm;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;

/**
 * 
 * This class is used to access all configured inferenz 
 * algorithms.
 * 
 * @author Mario Sänger (IT 09)
 * @version 1.0
 *
 */
public class InferenzAlgorithmRegistry {
	
	/**
	 * Algorithm extension point id
	 */
	private static final String XP_ID = "de.hwrberlin.remo.inferenz_algorithm";
	
	/**
	 * Extension Point fields
	 */
	
	private static final String NAME_ATTRIBUTE 		  = "name";
	private static final String DESCRIPTION_ATTRIBUTE = "description";
	private static final String CLASS_ATTRIBUTE 	  = "class";
	
	
	/**
	 * This method returns all configured inferenz algorithms.
	 * 
	 * @return
	 */
	public static List<InferenzAlgorithmXP> getRegisteredAlgorithms() {
		List<InferenzAlgorithmXP> algorithms = new ArrayList<InferenzAlgorithmXP>();
		InferenzAlgorithmXP algorithmXP;
		String name, description;
		Object algorithm;
		
		// Get the configuration for the algorithm extension point
		IConfigurationElement[] config = Platform.getExtensionRegistry()
												 .getConfigurationElementsFor(XP_ID);
		
		try {
			for (IConfigurationElement e : config) {
				
				// Get attributes from the configured extensions
				name = e.getAttribute(NAME_ATTRIBUTE);
				description = e.getAttribute(DESCRIPTION_ATTRIBUTE);
				algorithm = e.createExecutableExtension(CLASS_ATTRIBUTE);
				
				// Validate the values of the algorithm extension 
				if(name != null && name.length() > 0 && description != null && 
				   description.length() > 0 && algorithm instanceof InferenzAlgorithm) {
					
					// Build algorithm extension point wrapper
					algorithmXP = new InferenzAlgorithmXP(name, description, 
							(InferenzAlgorithm) algorithm);
					algorithms.add(algorithmXP);
				}
			}
		} catch (CoreException ex) {
			System.out.println(ex.getMessage());
		}
		
		return algorithms;
	}
}